<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>合作机构</cite></a>
    <a><cite>代理列表</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" method="post" lay-filter="form_agent_search">
        <script type="text/html" template>
          <div class="layui-inline">
            <div class="layui-form-label">关键字搜索</div>
            <div class="layui-input-inline">
              <input type="text" name="keyword" value="{{decodeURIComponent(layui.router().search.keyword || '')}}"
                     class="layui-input" placeholder="请输入代理商名称">
            </div>
          </div>
          <div class="layui-inline layui-btn-group">
            <button type="submit" lay-submit class="layui-btn" lay-filter="submit_agent_search">
              <i class="layui-icon layui-icon-search"></i>查询
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                    lay-filter="reset_agent_search">
              <i class="layui-icon layui-icon-close"></i>重置
            </button>
            <button id="btn_add_agent" class="layui-btn layui-btn-normal">
              <i class="layui-icon layui-icon-add-1"></i>添加
            </button>
          </div>
        </script>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_agent"></div>
      <script type="text/html" id="tpl_admin_user">
        {{d.admin_user.username}}
      </script>
      <script type="text/html" id="tpl_appid">
        <a class="layui-table-link" lay-tips="点击查看Secret" lay-event="show_secret">{{d.app_id}}</a>
      </script>
      <script type="text/html" id="tpl_status">
        {{# if (d.status.value === 'enabled') { }}
        <span class="layui-font-green">启用</span>
        {{# } }}
        {{# if (d.status.value === 'disabled') { }}
        <span class="layui-font-red">禁用</span>
        {{# } }}
      </script>
      <script type="text/html" id="tpl_tool">
        {{# if (d.status.value === 'enabled') { }}
        <a class="layui-table-link layui-font-red" lay-event="disable">
          禁用
        </a>
        {{# } }}
        {{# if (d.status.value === 'disabled') { }}
        <a class="layui-table-link layui-font-green" lay-event="enable">
          启用
        </a>
        {{# } }}
        <div class="ant-divider"></div>
        <a class="layui-table-link" lay-event="edit">
          <i class="layui-icon layui-icon-edit"></i>编辑
        </a>
        <div class="ant-divider"></div>
        <a class="layui-table-link" lay-href="agent/settings/id={{d.id}}">
          参数配置
        </a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.use(['fox_table'], function () {
    let $ = layui.$
      , fox_table = layui.fox_table
      , admin = layui.admin
      , form = layui.form
      , view = layui.view

    let resourceUrl = 'admin/agents'

    // 列表查询
    fox_table.render({
      id: 'table_agent'
      , url: resourceUrl
      , cols: [[
        {title: 'ID', field: 'id', width: 80, align: "center", sort: true}
        , {title: '代理商名称', field: 'name', minWidth: 100}
        , {title: '登录用户名', templet: '#tpl_admin_user', width: 150}
        , {title: 'APPID', templet: '#tpl_appid', width: 150}
        , {title: '添加时间', field: 'created_at', width: 180, align: "center"}
        , {title: '状态', templet: '#tpl_status', width: 100, align: "center"}
        , {title: '操作', width: 250, align: 'left', fixed: 'right', toolbar: '#tpl_tool'}
      ]]
    });

    //监听操作栏
    fox_table.toolbar(function (obj) {
      let data = obj.data;
      if (obj.event === 'edit') {
        showForm(data)
      }
      if (obj.event === 'show_secret') {
        showSecretForm(data)
      }
      if (obj.event === 'enable') {
        layer.confirm('确认启用该代理商?', function (index) {
          admin.post(resourceUrl + '/' + data.id + '/status', {status: 'enabled'}, function (res) {
            layer.close(index);
            fox_table.reload(); //重载表格
          })
        });
      }
      if (obj.event === 'disable') {
        layer.confirm('确认禁用该代理商?', function (index) {
          admin.post(resourceUrl + '/' + data.id + '/status', {status: 'disabled'}, function (res) {
            layer.close(index);
            fox_table.reload(); //重载表格
          })
        });
      }
    });

    //搜索
    form.on('submit(submit_agent_search)', function (d) {
      fox_table.reload(d.field);
      return false;
    });
    //重置
    form.on('submit(reset_agent_search)', function (d) {
      $("input[name='keyword']").val('');
      fox_table.reload({});
      return false;
    });
    //添加
    $('#btn_add_agent').click(function () {
      showForm();
      return false;
    });

    //显示弹窗
    function showForm(data) {
      data = data || {};
      let minWidth = '400px';
      let minHeight = '340px';
      admin.popup({
        title: (data.id ? '修改' : '添加') + '代理商'
        , area: [minWidth, minHeight]
        , id: 'popup_agent'
        , success: function (layero, index) {
          view(this.id).render('agent/form', data).done(function () {
            form.on('submit(submit_agent)', function (d) {
              let field = d.field;
              admin.post(resourceUrl, field, function (res) {
                layer.close(index);
                fox_table.reload();
              })
            })
          })
        }
      })
    }

    //显示APPID和SECRET
    function showSecretForm(data) {
      data = data || {};
      let minWidth = '500px';
      let minHeight = '240px';
      admin.popup({
        title: '查看APPID和SECRET'
        , area: [minWidth, minHeight]
        , id: 'popup_agent_appid'
        , success: function (layero, index) {
          view(this.id).render('agent/secret', data).done(function () {
          })
        }
      })
    }
  })
</script>

